package com.zhao.fancal;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhao.calculator.StringCalculator;

/**
 * 计算器
 */
public class Activity_main extends Activity {
    private final String TAG="ZHAO";
    private StringBuffer buffer;
    private StringCalculator sc;
    private TextView tv;
    private Button bt_init;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.bt_0:
                updateData('0');
                break;
            case R.id.bt_1:
                updateData('1');
                break;
            case R.id.bt_2:
                updateData('2');
                break;
            case R.id.bt_3:
                updateData('3');
                break;
            case R.id.bt_4:
                updateData('4');
                break;
            case R.id.bt_5:
                updateData('5');
                break;
            case R.id.bt_6:
                updateData('6');
                break;
            case R.id.bt_7:
                updateData('7');
                break;
            case R.id.bt_8:
                updateData('8');
                break;
            case R.id.bt_9:
                updateData('9');
                break;
            case R.id.bt_point:
                updateData('.');
                break;
            case R.id.bt_delete:
                updateData('d');
                break;
            case R.id.bt_add:
                updateData('+');
                break;
            case R.id.bt_minus:
                updateData('-');
                break;
            case R.id.bt_multiply:
                updateData('×');
                break;
            case R.id.bt_dividi:
                updateData('÷');
                break;
            case R.id.bt_equal:
                updateData('=');
                break;
            case R.id.bt_init:
                buffer=new StringBuffer();
                tv.setText(buffer);
                break;
        }
    }

    /**
     * 对程序初始化数据
     */
    protected void init(){
        tv=(TextView)findViewById(R.id.tv_display);
        buffer=new StringBuffer();
        tv.setText(buffer);
        sc=new StringCalculator();
        bt_init=(Button)findViewById(R.id.bt_init);
        /**
         * FanCal按钮弹出对话框
         */
        bt_init.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog ad= new AlertDialog.Builder(Activity_main.this).setTitle("FanCal")
                        .setMessage("送给杨凡的计算器")
                        .create();
                ad.show();
                return false;
            }
        });
        Log.d(TAG, "初始化");
    }
    /**
     * 添加字符到显示框的末尾
     * @param ch
     */
    protected void updateData(char ch){
        if (ch=='='){
            Log.d(TAG,"on =");
            if (tv.getText().equals("521+")){
                Log.d(TAG,buffer.toString());
                Toast.makeText(getApplicationContext(),"杨凡是个好女孩 所以我喜欢",
                        Toast.LENGTH_LONG).show();
                return;
            }
            try {
                double d=sc.cal(buffer+"");
                buffer=new StringBuffer();
                buffer.append(d);
                dis();
                buffer=new StringBuffer();
            } catch (Exception e) {
                buffer=new StringBuffer();
                tv.setText("错误输入");
                dis();
            }
            return;
        }
        if (ch=='d'){
            if (buffer.length()==0) {
                return;
            }
            buffer.deleteCharAt(buffer.length()-1);
            dis();
            return;
        }
        if (buffer.length()==0&&tv.getText().length()!=0){
            if (ch<='9'&&ch>='0'){
                dis();
            }else{
                buffer.append(tv.getText());
            }
        }
        buffer.append(ch);
        dis();
    }

    /**
     * 更新tv的显示数据
     */
    protected void dis(){
        tv.setText(buffer);
    }
}
